

// 验证内容
var strategies = {
	// 表单验证内容
	isNonEmpty: function(value, voids, errorMsg) {  // 验证是否必填 内容不能为空
		if(value === '' || value == voids) {
			return errorMsg
		}
	},
	isDefault: function(value, errorMsg){  // 是否为默认下拉选项
		if(value == $(this).attr('data-default')) {
			return errorMsg
		}
	},
	isMobile: function(value, errorMsg) {  // 验证手机格式
		if(value != '' && !/^((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8}$/.test(value)) {
			return errorMsg
		}
	},
	testEmail: function(value, errorMsg) {  // 验证邮箱格式
		if(value != '' && !/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value)) {
			return errorMsg
		}
	},
	isCardNo: function(value, errorMsg) {  // 验证身份证格式
		if(value != '' && !/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/.test(value) ) {
			return errorMsg
		}
	},
	isQQ: function(value, errorMsg) {  // 验证qq格式
		if(value != '' && !/[1-9][0-9]{4,}/.test(value)) {
			return errorMsg
		}
	},
	isAgreement: function(value, errorMsg) {  // 验证协议是否勾选
		if(!$(this).is('.checked')) {
			return errorMsg
		}
	},
	e:''  // dom标记
}

// 定义Validator类
var Validator = function() {  
	this.cache = []
}

Validator.prototype.add = function(dom, rule, errorMsg) {	
	var ary = rule.split(':')
	this.cache.push(function() {
	var strategy = ary.shift()
	ary.unshift($(dom).val())
	ary.push(errorMsg)
	return strategies[ strategy ].apply($(dom), ary)
	})
}

Validator.prototype.start = function() {
	for (var i = 0,validatorFunc;validatorFunc = this.cache[i++];) {
		var msg = validatorFunc()
		if(msg) {
		return msg
		}
	}
}

var validataFunc = function(rule) {
	var validator = new Validator()
	// 添加校验规则
	rule.forEach(function(item) {
		if($(item.dom).val() != undefined){
			validator.add(item.dom, item.type, item.msg)
		}
	});	
	var errorMsg = validator.start()
	return errorMsg
}


var onSubmit = function (dom, extend) {
	strategies.e = dom
	var rule = [
		{dom:$(dom + ' [name="custom"]'),type:'isNonEmpty:', msg:'姓名不能为空'},
		{dom:$(dom + ' [name="company"]'),type:'isNonEmpty:', msg:'所在单位不能为空'},
		{dom:$(dom + ' [name="mobile"]'),type:'isNonEmpty:', msg:'手机格不能为空'}, 
		{dom:$(dom + ' [name="mobile"]'),type:'isMobile', msg:'手机格式不正确'}, 
		{dom:$(dom + ' [name="id_card"]'),type:'isCardNo', msg:'身份证格式不正确'},
		{dom:$(dom + ' [name="email"]'),type:'testEmail', msg:'邮箱格式不正确'},
		{dom:$(dom + ' [name="qq"]'),type:'isQQ', msg:'qq格式不正确'},
		{dom:$(dom + ' [name="key"]'),type:'isNonEmpty:', msg:' 验证码不能为空'},
		{dom:$(dom + ' [name="id"]'),type:'isNonEmpty:', msg:'id不存在'},
		{dom:$(dom + ' [name="classid"]'),type:'isNonEmpty:', msg:'classid不存在'},
		{dom:$(dom + ' [name="token"]'),type:'isNonEmpty:', msg:'token不存在'},
		{dom:$(dom + ' [name="enews"]'),type:'isNonEmpty:', msg:'enews不存在'}
	];
	var errorMsg = validataFunc(rule)
	if (errorMsg) {
		alert(errorMsg)
		return false
	}

	var viaMsg = $(dom + ' [name="viaMsg"]').val() || ''; // 提交成功内容
	
	var data = {
		id: $(dom + ' [name="id"]').val(),  // 文章id
		classid: $(dom + ' [name="classid"]').val(), // 文章栏目id
		token: $(dom + ' [name="token"]').val(),  // 验证字符串
		company: $(dom + ' [name="company"]').val(),  // 所在单位
		name: $(dom + ' [name="name"]').val(),  // 姓名
		mobile: $(dom + ' [name="mobile"]').val(),  // 电话
		key: $(dom + ' [name="key"]').val(),  // 验证码
		enews: $(dom + ' [name="enews"]').val(),  // 固定值
		doajax: 1,
		lytext: $(dom + ' [name="lytext"]').val() || '',	  // 备注
		visa: getCheckSet($(dom + ' .checked [name="visa"]')) || getselect($(dom + ' [name="visa"]')), // 有无签证
	}; 
	
	sieve(data);  // 去除空值；
	if(extend){
		// 有特殊条件判断
		submitForm(data, false);
	} else {
		// 无特殊判断条件
		submitForm(data, true, viaMsg);
	}
}

// 籍贯内容
var ChineseDistricts = [
	{id:'110000',name: '北京市'},
	{id:'120000',name: '天津市'},
	{id:'130000',name: '河北省'},
	{id:'140000',name: '山西省'},
	{id:'150000',name: '内蒙古自治区'},
	{id:'210000',name: '辽宁省'},
	{id:'220000',name: '吉林省'},
	{id:'230000',name: '黑龙江省'},
	{id:'310000',name: '上海市'},
	{id:'320000',name: '江苏省'},
	{id:'330000',name: '浙江省'},
	{id:'340000',name: '安徽省'},
	{id:'350000',name: '福建省'},
	{id:'360000',name: '江西省'},
	{id:'370000',name: '山东省'},
	{id:'410000',name: '河南省'},
	{id:'420000',name: '湖北省'},
	{id:'430000',name: '湖南省'},
	{id:'440000',name: '广东省'},
	{id:'450000',name: '广西壮族自治区'},
	{id:'460000',name: '海南省'},
	{id:'500000',name: '重庆市'},
	{id:'510000',name: '四川省'},
	{id:'520000',name: '贵州省'},
	{id:'530000',name: '云南省'},
	{id:'540000',name: '西藏自治区'},
	{id:'610000',name: '陕西省'},
	{id:'620000',name: '甘肃省'},
	{id:'630000',name: '青海省'},
	{id:'640000',name: '宁夏回族自治区'},
	{id:'650000',name: '新疆维吾尔自治区'},
	{id:'710000',name: '台湾省'},
	{id:'810000',name: '香港特别行政区'},
	{id:'820000',name: '澳门特别行政区'}
]

var addressOption = function(dom) {
	for(var i = 0; i < ChineseDistricts.length; i++) {
        var op = '<option value="' + ChineseDistricts[i].name + '" >' + ChineseDistricts[i].name + '</option>';
        $(dom + ' select[name="birthplace"]').append(op);
    }
}

// ajax
var submitForm = function(params, asyncStart, alertMsg) {
	$.ajax({
		url: 'http://shjy.jtsf.cn/e/enews/index.php',
		type: 'post',
		data: params,
		dataType: 'json',
		success: function(res) {
			console.log(res)
			if(res.errCode == 0) {
				if(!asyncStart){
					specialTips(strategies.e)
				} else {
					alert(alertMsg)
				}
            } else {
				alert(res.errMsg);
            }   
		}
	})
}

var sieve = function(data) {
	for (var key in data) {
		if (data[key] === '') {
			delete data[key]
		}
	}
}
var getCheckSet = function(params){  // check 可多选
	var checkList = []
	for(var i = 0; i < params.length; i++){
		checkList.push($(params[i]).val()) 
	}
	return checkList +''
}

var getselect = function(params){  // select Value
	var val = params.val() || '';
	var initVal = params.attr('data-default') || '';
	return initVal == '' ? val : val == initVal ? '' : val 
}
